
<%@ page contentType="text/html;charset=utf-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jstl/core" %>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<%
    String path = request.getContextPath();
    String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN""http://www.w3.org/TR/html4/loose.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <base href="<%=basePath%>">
    <meta charset="UTF-8">
    <meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no">
    <meta name="format-detection" content="telephone=no">
    <link rel="stylesheet" type="text/css" href="<c:url value='/resource/css/main.css'/>"/>
    <script type="text/javascript" src="<c:url value='/resource/js/jquery-1.11.1.min.js'/>"></script>
    <script type="text/javascript" src="<c:url value='/resource/js/num-alignment.js'/>"></script>
    <!--input可增减JS-->
    <title>用户登录</title>
</head>
<body>
<div class="main_box">
    <div class="log_top">
        <p><img src="<%=basePath%>/resource/images/logo.png"></p>
        <p class="word">
            苏州技防工程信息管理系统
        </p>
    </div>
    <ul class="log_ipt">
        <form id="loginForm">
            <li><input  type="text" placeholder="请输入手机号" id="mobile" name="mobile"/></li>
            <li>
                <input class="yzm" style="-webkit-appearance:none;" type="button" value="获取验证码" onclick="GetSmsCode(this);" id="GetVeriCode"/>
                <input class="yzm_ipt" type="text" placeholder="请输入验证码" id="code" name="code"/>
            </li>
        </form>
    </ul>
    <input class="log_btn all_btn" type="submit" value="登录" id="login-btn"/>
</div>
</body>
<script type="text/javascript">
    //已经发送验证码时为1
     var veri_flag;
    //用户登录
    $(function () {
        $("#login-btn").click(function () {
            if(veri_flag==1){
                submitForm();
            }else{
                alert("请先获取验证码");
                return;
            }
        });

        function submitForm() {
            var _mobile = $.trim($("#mobile").val()), _code = $.trim($("#code").val());
            if (_mobile.length == 0) {
                alert("请输入手机号");
                return;
            }
            if (_code.length == 0) {
                alert("请输入验证码");
                return;
            }
            $.ajax({
                url: "profile/auth",
                type: "post",
                async: false,
                dataType: "json",
                data: "phone=" + _mobile + "&temp3=" + _code,
                success: function (resp) {
                    if (resp && resp.succ) {
                        window.location.href="profile/index";
                        <%--window.location.replace("<c:url value='/profile/index'/>");--%>
                    } else {
                        alert("验证码错误");
                        return false;
                    }
                }

            });
        }
    });
    //获取验证码
    function GetSmsCode(obj){
        if (!CheckPhone())
            return false;
        $.ajax({
            url: 'profile/getvericode',
            data: { phone: $("#mobile").val() },
            async: false,
            cache: false,
            type: 'Post',
            dataType:'json',
            beforeSend:function(){
                $(obj).attr("disabled", "true");
            },
            success: function (data) {
                if (data.statusCode == 1) {
                    alert("当前账号不存在");
                    $(obj).removeAttr("disabled");
                } else if (data.statusCode == 2) {
                    alert("当前账号不能进行登录");
                    $(obj).removeAttr("disabled");
                } else if(data.statusCode == 4){
                    $("#GetVeriCode").attr("disabled", "true");
                    veri_flag = 1;//发送成功
                    var timeSpan = 120;
                    Time(timeSpan == undefined || timeSpan == "" ? 120 : timeSpan);
                }
                //alert("短信已发送，请注意查收");

            },
            error:function(){
                $(obj).attr("disabled", "false");
                alert("请求失败");
            }
        });
    }
    //验证手机号
    function CheckPhone(){
        if ($.trim($("#mobile").val()).length == 0) {
            alert("请输入手机号码");
            return false;
        } else {
            var mobile = /^(((13[0-9]{1})|(15[0-9]{1})|(18[0-9]{1})|(14[0-9]{1})|(17[0-9]{1}))+\d{8})$/;
            if (!mobile.test($.trim($("#mobile").val()))) {
                alert("请输入正确的手机号码");
                return false;
            }
        }
        return true;
    }
    //倒计时
    function Time(t) {
        $("#GetVeriCode").val((t--) + "秒后过期");
        if (t == 0) {
            $("#GetVeriCode").val("重新获取");
            $("#GetVeriCode").removeAttr("disabled");
            clearTimeout(tm);
            veri_flag = 0;//验证码过期
        }
        else {
            tm = setTimeout("Time(" + t + ")", 1000);
        }
    }
</script>
</html>